"""
提供对房屋的各种操作
"""
from utils import *

house_list = [{
    "id": 2,
    "name": "tim",
    "phone": "113",
    "address": "海淀",
    "rent": 800,
    "status": "未出租"
}]


def main_menu():
    """
    显示主菜单，并让用户选择
    :return:
    """
    print()
    print("房屋出租系统菜单".center(50, "="))
    print("\t\t\t1. 新 增 房 源")
    print("\t\t\t2. 查 找 房 屋")
    print("\t\t\t3. 删 除 房 屋 信 息")
    print("\t\t\t4. 修 改 房 屋 信 息")
    print("\t\t\t5. 房 屋 列 表")
    print("\t\t\t6. 退 出")


def add_house():
    """
    添加房屋信息
    :return:
    """
    print("添加房屋".center(50, "="))
    name = input("姓名：")
    phone = input("电话：")
    address = input("地址：")
    rent = int(input("月租："))
    status = input("状态：")
    house = {
        "id": max([house['id'] for house in house_list]) + 1,
        "name": name,
        "phone": phone,
        "address": address,
        "rent": rent,
        "status": status
    }
    house_list.append(house)
    print("添加房屋成功".center(50, "="))


def find_house():
    """
    查找房屋信息
    :return:
    """
    print("查找房屋信息".center(50, "="))
    num = int(input("请输入要查找的房屋编号（-1退出）："))
    if num == -1:
        print("放弃查找房屋信息".center(50, "="))
        return
    house = find_by_id(num)
    if house:
        print("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)")
        print(
            f"{house['id']}\t\t{house['name']}\t\t{house['phone']}\t\t{house['address']}\t\t{house['rent']}\t\t{house['status']}")
        print("查找房屋信息成功".center(50, "="))
    else:
        print("房屋编号不存在，查找失败".center(50, "="))


def del_house():
    """
    删除房屋信息
    :return:
    """
    print("删除房屋信息".center(50, "="))
    num = int(input("请输入要删除的房屋编号（-1退出）："))

    if num == -1:
        print("放弃删除房屋信息".center(50, "="))
        return

    if read_confirm_select().upper() == "Y":
        # 根据 del_id 去 hoses 列表查找是否存在该房屋
        house = find_by_id(num)
        if house:
            house_list.remove(house)
            print("删除房屋信息成功".center(50, "="))
        else:
            print("房屋编号不存在，删除失败".center(50, "="))
    else:
        print("放弃删除房屋信息".center(50, "="))


def update_house():
    """
    更新房屋信息
    :return:
    """
    print("更新房屋信息".center(50, "="))
    num = int(input("请输入要更新的房屋编号（-1退出）："))
    if num == -1:
        print("放弃更新房屋信息".center(50, "="))
        return
    house = find_by_id(num)
    if not house:
        print("房屋编号不存在，更新失败".center(50, "="))
        return
    house["name"] = input("姓名：")
    house["phone"] = input("电话：")
    house["address"] = input("地址：")
    house["rent"] = int(input("月租："))
    house["status"] = input("状态：")
    print("更新房屋信息成功".center(50, "="))


def list_houses():
    """
    列出所有的房屋信息
    :return:
    """
    print()
    print("房屋列表".center(50, "="))
    print("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)")
    for house in house_list:
        print(
            f"{house['id']}\t\t{house['name']}\t\t{house['phone']}\t\t{house['address']}\t\t{house['rent']}\t\t{house['status']}")
    print("房屋列表显示完毕".center(50, "="))


def exit_sys():
    """
    退出系统
    :return:
    """
    if read_confirm_select().upper() == "Y":
        return True
    else:
        return False

def find_by_id(find_id):
    """
    根据输入的 find_id 返回对应的房屋信息
    :param find_id: 要查找的房屋编号
    :return: 房屋信息，不存在返回 None
    """
    for house in house_list:
        if house["id"] == find_id:
            return house
    return None